package net.huashitong.synthetical.newSupervision.controller;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.sys.service.*;
import com.sirdc.modules.sys.util.SysUserUtils;
import net.huashitong.synthetical.newSupervision.entity.TaskNew;
import net.huashitong.synthetical.newSupervision.entity.TaskSend;
import net.huashitong.synthetical.newSupervision.entity.TaskWorkable;
import net.huashitong.synthetical.newSupervision.filter.TaskNewFilter;
import net.huashitong.synthetical.newSupervision.service.TaskLogService;
import net.huashitong.synthetical.newSupervision.service.TaskNewService;
import net.huashitong.synthetical.newSupervision.service.TaskSendService;
import net.huashitong.synthetical.newSupervision.service.TaskWorkableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: ling
 * Date: 2019/7/10
 * Time: 15:44
 * To change this template use File | Settings | File Templates.
 * Description:
 */
@Controller
@RequestMapping("/taskWorkable")
public class TaskWorkableController extends JsonBaseController {

    @Autowired
    private TaskSendService taskSendService;

    @Autowired
    private TaskNewService taskNewService;

    @Autowired
    private TaskLogService taskLogService;

    @Autowired
    private TaskWorkableService taskWorkableService;

    @Autowired
    private SysUserPermissionService sysUserPermissionService;

    @Autowired
    private SysDeptService sysDeptService;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysLoginService sysLoginService;

    @Autowired
    private DeptService deptService;

    @Override
    protected String getView(String s) {
        return "/modules/project/taskWorkable/" + s;
    }

    @RequestMapping(method = RequestMethod.GET)
    public String initPage() {
        return getView("query");
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST)
    public List taskByUser(@ModelAttribute TaskNewFilter taskNewFilter) {
        String userId = SysUserUtils.getUserId();
        List<TaskNew> projectList = new ArrayList<>();
        List<TaskWorkable> workableList = taskWorkableService.queryByUserId(userId);
        for (TaskWorkable workable : workableList) {
            String taskId = workable.getTaskId();
            TaskNew taskNew = taskNewService.getById(taskId);
            projectList.add(taskNew);
        }
        return projectList;
    }

    /**
     * 查看详情
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
    public String detail(Model model, @PathVariable String id) {
        TaskNew task = taskNewService.getById(id);
        List<TaskSend> sendList = taskSendService.queryByTaskId(id);
        List<TaskWorkable> workableList = taskWorkableService.queryByTaskId(id);
        model.addAttribute("taskNew", task);
        model.addAttribute("sendList", sendList);
        model.addAttribute("workableList", workableList);
        return getView("detail");
    }

}
